<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //检测2-位汉字
      function isLengthInRange(char) {
        var reg = /^[\u4e00-\u9fa5]{2,4}$/;
        let result = reg.test(char);
        if (result) {
          console.log(char, "汉字个数在2-4范围内");
        } else {
          console.log(char, "不在范围内");
        }
      }
      isLengthInRange("汉字字母");
      isLengthInRange("汉字");
      isLengthInRange("汉字字母在");
      isLengthInRange("汉");
    </script>
  </body>
</html>
